<template>
  <div class="search-container" v-loading="loading">
    <el-form v-model="searchPage" inline="true" >
      <el-form-item  v-for="(item, index) in searchPage.items" :key="index" :label="item.cnname">
        <el-input v-if="item.itemType !== 'DATE'" v-model="item.value"></el-input>
        <el-col :span="24" v-else>
          <el-form-item>
            <el-date-picker
              v-model="item.dateValue"
              type="date"
              label="Pick a date"
              placeholder="Pick a date"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button @click="onReSend">提醒短信</el-button>
        <el-button @click="onImport">导入</el-button>
        <el-button v-show="false" @click="onDownloadTemplate">导出模板</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="data-container">
      <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" width="80" label="序号"/>
      <el-table-column prop="schoolYear" label="学年" width="100" />
      <el-table-column prop="term" label="学期" width="80" />
      <el-table-column prop="cycle" label="周次" width="80" />
      <el-table-column prop="classDate" label="上课日期" width="120" />
      <el-table-column prop="week" label="周次" width="80" />
      <el-table-column prop="section" label="节次" width="100" />
      <el-table-column prop="address" label="授课地点" width="180" />
      <el-table-column prop="courseName" label="课程名称" width="120" />
      <el-table-column prop="teachContent" label="授课内容" width="320" />
      <el-table-column prop="teacher" label="授课老师" width="120" />
      <el-table-column prop="teachTel" label="授课老师电话" width="120" />
      <el-table-column prop="teachObject" label="授课对象" width="120" />
      <el-table-column prop="stuNums" label="听课人数" width="60" />
      <el-table-column prop="courseSecretary" label="课程秘书" width="120" />
      <el-table-column prop="courseSecretaryTel" label="课程秘书电话" width="120" />
      <el-table-column prop="academicSecretary" label="课程秘书" width="120" />
      <el-table-column prop="academicSecretaryTel" label="课程秘书电话" width="120" />
      <el-table-column prop="sendStatus" label="发送状态" width="120">
        <template #default="scope">
          <el-select v-model="scope.row.sendStatus" placeholder="" name="sendStatus" disabled >
            <el-option label="已发送" value="true" />
            <el-option label="未发送" value="false" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="140">
        <!-- vue3中 solt-scop已经放弃使用 -->
        <template #default="scope">
          <el-button text size="small" @click="handleClick(scope.row)">编辑</el-button>
          <el-button text type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
     <el-pagination
      v-model:currentPage="searchPage.current"
      v-model:page-size="searchPage.pageSize"
      :page-sizes="[20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableDatas.total"
      hide-on-single-page=true
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
  <div>
    <import-excel v-model="modelValue" :uploadUrl="uploadUrl"></import-excel>
  </div>
  <div>
    <!--<edit-form v-model="showEditModel" :schedule="currentSchedule"></edit-form>-->
    <el-dialog
      v-model="dialogVisible"
      title="编辑"
      width="60%"
    >
      <el-form :model="currentSchedule" ref="scheduleRef" label-width="360px" label-position="top">
        <el-form-item label="学年">
          <el-input v-model="currentSchedule.schoolYear" />
        </el-form-item>
        <el-form-item label="学期">
          <el-input v-model.number="currentSchedule.term" />
        </el-form-item>
        <el-form-item label="周次">
          <el-input v-model="currentSchedule.cycle" />
        </el-form-item>
        <el-form-item label="上课日期" required>
          <el-col :span="12">
            <el-form-item>
              <el-date-picker
                v-model="currentSchedule.classDate"
                type="date"
                placeholder=""
                style="width: 100%"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="星期">
          <el-input v-model="currentSchedule.week" />
        </el-form-item>
        <el-form-item label="节次">
          <el-input v-model="currentSchedule.section" />
        </el-form-item>
        <el-form-item label="授课地点">
          <el-input v-model="currentSchedule.address" />
        </el-form-item>
        <el-form-item label="课程名称">
          <el-input v-model="currentSchedule.courseName" />
        </el-form-item>
        <el-form-item label="课程内容">
          <el-textarea v-model="currentSchedule.teachContent"></el-textarea>
        </el-form-item>
        <el-form-item label="授课老师">
          <el-input v-model="currentSchedule.teacher" />
        </el-form-item>
        <el-form-item label="老师电话">
          <el-input v-model="currentSchedule.teachTel" />
        </el-form-item>
        <el-form-item label="授课对象">
          <el-input v-model="currentSchedule.teachObject" />
        </el-form-item>
        <el-form-item label="听课人数">
          <el-input v-model.number="currentSchedule.stuNums" />
        </el-form-item>
        <el-form-item label="课程秘书">
          <el-input v-model="currentSchedule.courseSecretary" />
        </el-form-item>
        <el-form-item label="课程秘书电话">
          <el-input v-model="currentSchedule.courseSecretaryTel" />
        </el-form-item>
        <el-form-item label="教务秘书">
          <el-input v-model="currentSchedule.academicSecretary" />
        </el-form-item>
        <el-form-item label="教务秘书电话">
          <el-input v-model="currentSchedule.academicSecretaryTel" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSave(currentSchedule.id)">保存</el-button>
          <!-- <el-button @click="onReset">{{$t('msg.common.reset')}}</el-button> -->
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onActivated } from 'vue'
import { list, deleteById, modify, sendMsg } from '@/api/xmu/schedule'
import { dateToString, dateToNext, dateTimeToString } from '@/utils/date'
import { ElMessage } from 'element-plus'
import { useI18n } from 'vue-i18n'
import ImportExcel from '@/components/ImportExcel.vue'
import editForm from '../form/index.vue'

const tableDatas = ref({})
// 导入的显示控制
const modelValue = ref(false)

const showEditModel = ref(false)
// 修改的显示控制
const dialogVisible = ref(false)

const currentSchedule = ref({})

const loading = ref(false)

const uploadUrl = process.env.VUE_APP_BASE_API + '/schedule/import'

const i18n = useI18n()

const searchPage = ref({
  items: [
    {
      name: 'teacher',
      cnname: '教师名称',
      itemType: 'STRING',
      sign: 'like',
      value: ''
    },
    {
      name: 'courseName',
      cnname: '课程名称',
      itemType: 'STRING',
      sign: 'like',
      value: ''
    },
    {
      name: 'teachObject',
      cnname: '授课对象',
      itemType: 'STRING',
      sign: 'like',
      value: ''
    },
    {
      name: 'classDate1',
      cnname: '开始时间',
      itemType: 'DATE',
      sign: '>=',
      value: dateToString(-7),
      dateValue: dateToNext(-7)
    },
    {
      name: 'classDate2',
      cnname: '结束时间',
      itemType: 'DATE',
      sign: '<=',
      value: dateToString(0),
      dateValue: new Date()
    }
  ],
  pageSize: 20,
  current: 1,
  sortBy: ['id']
})

const tableData = ref([])

const searchFunction = async () => {
  list(searchPage.value).then(res => {
    tableData.value = res.content
    tableDatas.value.total = parseInt(res.totalElements)
    ElMessage.success('成功')
  }).catch(err => {
    ElMessage.success('失败' || err.message)
  })
}

onActivated(searchFunction())

const onSubmit = () => {
  searchFunction()
}

const onReset = () => {
  searchPage.value = {
    items: [
      {
        name: 'teacher',
        cnname: '教师名称',
        itemType: 'STRING',
        sign: 'like',
        value: ''
      },
      {
        name: 'courseName',
        cnname: '课程名称',
        itemType: 'STRING',
        sign: 'like',
        value: ''
      },
      {
        name: 'teachObject',
        cnname: '授课对象',
        itemType: 'STRING',
        sign: 'like',
        value: ''
      },
      {
        name: 'classDate1',
        cnname: '开始时间',
        itemType: 'DATE',
        sign: '>=',
        value: dateToString(-7),
        dateValue: dateToNext(-7)
      },
      {
        name: 'classDate2',
        cnname: '结束时间',
        itemType: 'DATE',
        sign: '<=',
        value: dateToString(0),
        dateValue: new Date()
      }
    ],
    pageSize: 20,
    current: 1,
    sortBy: ['id']
  }
  // 处理查询
  searchFunction()
}

const onImport = () => {
  modelValue.value = true
}

const handleSizeChange = pageSize => {
  searchPage.value.pageSize = pageSize
  // 执行查询操作
  searchFunction()
}

const handleCurrentChange = current => {
  searchPage.value.current = current
  // 执行查询操作
  searchFunction()
}

const handleClick = row => {
  dialogVisible.value = true
  currentSchedule.value = row
}

const onReSend = () => {
  sendMsg().then(res => {
    ElMessage.success('成功')
  }).catch(err => {
    loading.value = false
    ElMessage.error(err.message)
  })
}

const handleDelete = row => {
  loading.value = true
  deleteById(row.id).then(res => {
    // console.log(res)
    ElMessage.success('成功')
    searchFunction()
    loading.value = false
  }).catch(err => {
    loading.value = false
    ElMessage.error(err.message)
  })
}

const onSave = id => {
  // console.log(currentSchedule.value)
  // console.log(id)
  modify(currentSchedule.value, id).then(res => {
    dialogVisible.value = false
    ElMessage.success('修改成功')
  }).catch(err => {
    ElMessage.error(err.message)
  })
}
</script>

<style lang="scss" scope>
.search-container {
  width: 100%;
  height: 100px;
}
</style>
